package com.spa.domain.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.domain.entity.Carousel;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.mapper.CarouselServiceMapperStruct;
import com.spa.infrastructure.repository.condition.CarouselCondition;
import com.spa.infrastructure.repository.po.CarouselPO;
import com.spa.infrastructure.repository.service.CarouselRepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 轮播图  domain
 *
 * @author: lig
 * @date: 2023-12-22
 */
@Service
public class CarouselService {

    @Resource
    private CarouselRepositoryService carouselRepositoryService;

    /**
     * 列表
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public List<Carousel> listAll(CarouselCondition condition) {
        return CarouselServiceMapperStruct.INSTANCE.po2Entity4List(carouselRepositoryService.listAll(condition));
    }

    /**
     * 列表
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public Page<Carousel> page(CarouselCondition condition) {
        return CarouselServiceMapperStruct.INSTANCE.po2Entity4Page(carouselRepositoryService.page(condition));
    }

    /**
     * 获取对象
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public Carousel infoById(Long id) {
        return CarouselServiceMapperStruct.INSTANCE.po2Entity(carouselRepositoryService.getById(id));
    }

    /**
     * 添加
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public boolean add(CarouselPO po) {
        return carouselRepositoryService.save(po);
    }

    /**
     * 修改
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public boolean edit(CarouselPO po) {
        return carouselRepositoryService.updateById(po);
    }

    /**
     * 上下架显示
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public boolean onOffShow(Long id) {
        CarouselPO po = carouselRepositoryService.getById(id);
        if (BeanUtil.isNotEmpty(po)) {
            CarouselPO u = new CarouselPO();
            u.setId(id);
            if (po.getIsShow().equals(WhetherTypeEnum.$0.getCode())) {
                u.setIsShow(WhetherTypeEnum.$1.getCode());
            } else {
                u.setIsShow(WhetherTypeEnum.$0.getCode());
            }
            return carouselRepositoryService.updateById(u);
        }
        return false;

    }
    /**
     * 删除
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public boolean delById(Long id) {
        return carouselRepositoryService.removeById(id);
    }

}
